package com.qf.flash.ui.mine;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.R;
import com.qf.flash.event.GetCashEvent;
import com.qf.flash.http.HttpManager;
import com.qf.flash.model.AlipayInfoBean;
import com.qf.flash.model.TiXianBean;
import com.qf.flash.mvp.contract.TiXianContract;
import com.qf.flash.mvp.presenter.TiXianPresenter;
import com.qf.flash.utils.AppToast;
import com.qf.flash.utils.MyUtil;
import com.qf.flash.widget.ClearEditText;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.util.RxUtils;

import org.greenrobot.eventbus.EventBus;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class GetCashActivity extends BaseActivity implements TiXianContract.View {
    @BindView(R.id.tv_top_back)
    TextView tvTopBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.et_account)
    TextView etAccount;
    @BindView(R.id.et_take_money)
    ClearEditText etTakeMoney;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.tv_take_all)
    TextView tvTakeAll;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    private TiXianPresenter tiXianPresenter;
    private String type;
    private TiXianBean tiXianBean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_get_cash;
    }

    @Override
    protected void initInject() {
        super.initInject();
        tiXianPresenter = new TiXianPresenter();
        initPresenters(tiXianPresenter);
        type = getIntent().getStringExtra("type");
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("提现");
        tiXianPresenter.getMoney(type);
    }

    public static void start(Activity activity, String type) {
        Intent intent = new Intent(activity, GetCashActivity.class);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

    @OnClick({R.id.tv_top_back, R.id.tv_take_all, R.id.tv_sure,R.id.et_account})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.et_account:
                if (TextUtils.isEmpty(etAccount.getText().toString())) {
                    HttpManager.getInstance().getGsonHttpApi()
                            .GET_ALIPAY_MSG(UserMgr.getInstance().getLoginUser().getUid())
                            .compose(RxUtils.handleResult())
                            .compose(RxUtils.io_main())
                            .subscribe(new Consumer<AlipayInfoBean>() {
                                @Override
                                public void accept(AlipayInfoBean alipayInfoBean) throws Exception {
                                    BindAlipayActivity.start(GetCashActivity.this, alipayInfoBean);
                                    finish();
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) throws Exception {
                                    FlashLib.showToast("获取账户信息失败");
                                }
                            });
                }
                break;
            case R.id.tv_take_all:
                if (this.tiXianBean == null)
                    return;
                DecimalFormat df = new DecimalFormat("#0.00");
                String moneyStr = df.format(Double.parseDouble(tiXianBean.getMoney()));
                etTakeMoney.setText(moneyStr);
                break;
            case R.id.tv_sure:
                String account = tiXianBean.getBank().getId();
                String money = etTakeMoney.getText().toString().trim();
                if (!MyUtil.isNullOrEmpty(account) && !MyUtil.isNullOrEmpty(money)) {
                    tiXianPresenter.startTakeMoney(account, type, Double.parseDouble(money));
                } else {
                    AppToast.showToast("信息填写错误！");
                }

                break;
        }
    }

    @Override
    public void setMyMoney(TiXianBean tiXianBean) {
        this.tiXianBean = tiXianBean;
        if (this.tiXianBean == null)
            return;
        DecimalFormat df = new DecimalFormat("#0.00");
        String money = df.format(Double.parseDouble(tiXianBean.getMoney()));
        tvBalance.setText(String.format("当前余额：%s元", money));
        TiXianBean.BankBean bank = tiXianBean.getBank();
        if (bank != null && !TextUtils.isEmpty(bank.getBankCard()))
            etAccount.setText(bank.getBankCard());
        else
            etAccount.setHint("请先绑定支付宝账号");

    }

    @Override
    public void getCashSuccess() {
        EventBus.getDefault().post(new GetCashEvent());
        tiXianPresenter.getMoney(type);
    }

}
